
* Be sure working directory is set to location where dta files are stored
use DO_ASEC_data.dta, clear

* Save some estimates to excel - they will be called later using Matlab 
putexcel set DO_ASEC_results.xlsx, replace  


* Drop those younger than 16 in the first year observed
drop if age_1<16

* Adjust income values for inflation (Convert to Constant 1999 dollar values using "cpi99", see https://cps.ipums.org/cps/cpi99.shtml)
gen adj_inclongj_1 = inclongj_1*cpi99_1
gen adj_inclongj_2 = inclongj_2*cpi99_2


* Generate control dummy variables for sex, race, citizenship, education, occupation
gen male=0
replace male=1 if sex_1==1

gen white=0
replace white=1 if race_1==100
gen black=0
replace black=1 if race_1==200
gen asian=0
replace asian=1 if race_1==651
gen namerican = 0
replace namerican = 1 if race_1==652 | race_1==300


gen uscitizen=0
replace uscitizen=1 if citizen_1 == 1 | citizen_1 == 2 | citizen_1 == 3 | citizen_1 == 4 

gen noGED = 0
replace noGED=1 if educ_1<=72
*-gen hs_only = 0
*-replace hs_only=1 if educ_1==73
gen some_college = 0 
replace some_college=1 if educ_1>73 & educ_1<111
gen bachelors = 0
replace bachelors=1 if educ_1>=111

gen edu_group =1 if noGED==1
replace edu_group = 2 if noGED==0 & some_college==0 & bachelors==0
replace edu_group = 3 if some_college==1
replace edu_group = 4 if bachelors==1
label define edu_grp2 1 "No GED" 2 "High School" 3 "Some College" 4 "Bachelor's Degree"
label values edu_group edu_grp2
label variable edu_group "Education Category"

*- occupations and occupation switchers
gen occ_switch = 0
* occ 1
gen occ_manage_1 = 0  
replace occ_manage_1 = 1 if occ10ly_1>=0010 & occ10ly_1<=0430
gen occ_manage_2 = 0
replace occ_manage_2 = 1 if occ10ly_2>=0010 & occ10ly_2<=0430
replace occ_switch = 1 if occ_manage_1 == 1 & occ_manage_2 != 1
* occ 2
gen occ_business_1 = 0
replace occ_business_1 = 1 if occ10ly_1>=0500 & occ10ly_1<=0730
gen occ_business_2 = 0
replace occ_business_2 = 1 if occ10ly_2>=0500 & occ10ly_2<=0730
replace occ_switch = 1 if occ_business_1 == 1 & occ_business_2 != 1
* occ 3
gen occ_finance_1 = 0
replace occ_finance_1 = 1 if occ10ly_1>=0800 & occ10ly_1<=0950
gen occ_finance_2 = 0
replace occ_finance_2 = 1 if occ10ly_2>=0800 & occ10ly_2<=0950
replace occ_switch = 1 if occ_finance_1 == 1 & occ_finance_2 != 1
* occ 4
gen occ_computers_1 = 0
replace occ_computers_1 = 1 if occ10ly_1>=1000 & occ10ly_1<=1240
gen occ_computers_2 = 0
replace occ_computers_2 = 1 if occ10ly_2>=1000 & occ10ly_2<=1240
replace occ_switch = 1 if occ_computers_1 == 1 & occ_computers_2 != 1
* occ 5
gen occ_engineertech_1 = 0
replace occ_engineertech_1 = 1 if occ10ly_1>=1300 & occ10ly_1<=1560
gen occ_engineertech_2 = 0
replace occ_engineertech_2 = 1 if occ10ly_2>=1300 & occ10ly_2<=1560
replace occ_switch = 1 if occ_engineertech_1 == 1 & occ_engineertech_2 != 1
* occ 6
gen occ_soc_1 = 0
replace occ_soc_1 = 1 if occ10ly_1>=1600 & occ10ly_1<=1980
gen occ_soc_2 = 0
replace occ_soc_2 = 1 if occ10ly_2>=1600 & occ10ly_2<=1980
replace occ_switch = 1 if occ_soc_1 == 1 & occ_soc_2 != 1
* occ 7
gen occ_comm_1 = 0
replace occ_comm_1 = 1 if occ10ly_1>=2000 & occ10ly_1<=2060
gen occ_comm_2 = 0
replace occ_comm_2 = 1 if occ10ly_2>=2000 & occ10ly_2<=2060
replace occ_switch = 1 if occ_comm_1 == 1 & occ_comm_2 != 1
* occ 8
gen occ_legal_1 = 0
replace occ_legal_1 = 1 if occ10ly_1>=2100 & occ10ly_1<=2150
gen occ_legal_2 = 0
replace occ_legal_2 = 1 if occ10ly_2>=2100 & occ10ly_2<=2150
replace occ_switch = 1 if occ_legal_1 == 1 & occ_legal_2 != 1
* occ 9
gen occ_educ_1 = 0
replace occ_educ_1 = 1 if occ10ly_1>=2200 & occ10ly_1<=2550
gen occ_educ_2 = 0
replace occ_educ_2 = 1 if occ10ly_2>=2200 & occ10ly_2<=2550
replace occ_switch = 1 if occ_educ_1 == 1 & occ_educ_2 != 1
* occ 10
gen occ_arts_1 = 0
replace occ_arts_1 = 1 if occ10ly_1>=2600 & occ10ly_1<=2920
gen occ_arts_2 = 0
replace occ_arts_2 = 1 if occ10ly_2>=2600 & occ10ly_2<=2920
replace occ_switch = 1 if occ_arts_1 == 1 & occ_arts_2 != 1
* occ 11
gen occ_health_1 = 0
replace occ_health_1 = 1 if occ10ly_1>=3000 & occ10ly_1<=3540
gen occ_health_2 = 0
replace occ_health_2 = 1 if occ10ly_2>=3000 & occ10ly_2<=3540
replace occ_switch = 1 if occ_health_1 == 1 & occ_health_2 != 1
* occ 12
gen occ_healthsupp_1 = 0
replace occ_healthsupp_1 = 1 if occ10ly_1>=3600 & occ10ly_1<=3650
gen occ_healthsupp_2 = 0
replace occ_healthsupp_2 = 1 if occ10ly_2>=3600 & occ10ly_2<=3650
replace occ_switch = 1 if occ_healthsupp_1 == 1 & occ_healthsupp_2 != 1
* occ 13
gen occ_protective_1 = 0
replace occ_protective_1 = 1 if occ10ly_1>=3700 & occ10ly_1<=3950
gen occ_protective_2 = 0
replace occ_protective_2 = 1 if occ10ly_2>=3700 & occ10ly_2<=3950
replace occ_switch = 1 if occ_protective_1 == 1 & occ_protective_2 != 1
* occ 14
gen occ_food_1 = 0
replace occ_food_1 = 1 if occ10ly_1>=4000 & occ10ly_1<=4150
gen occ_food_2 = 0
replace occ_food_2 = 1 if occ10ly_2>=4000 & occ10ly_2<=4150
replace occ_switch = 1 if occ_food_1 == 1 & occ_food_2 != 1
* occ 15
gen occ_building_1 = 0
replace occ_building_1 = 1 if occ10ly_1>=4200 & occ10ly_1<=4250
gen occ_building_2 = 0
replace occ_building_2 = 1 if occ10ly_2>=4200 & occ10ly_2<=4250
replace occ_switch = 1 if occ_building_1 == 1 & occ_building_2 != 1
* occ 16
gen occ_care_1 = 0
replace occ_care_1 = 1 if occ10ly_1>=4300 & occ10ly_1<=4650
gen occ_care_2 = 0
replace occ_care_2 = 1 if occ10ly_2>=4300 & occ10ly_2<=4650
replace occ_switch = 1 if occ_care_1 == 1 & occ_care_2 != 1
* occ 17
gen occ_sales_1 = 0
replace occ_sales_1 = 1 if occ10ly_1>=4700 & occ10ly_1<=4965
gen occ_sales_2 = 0
replace occ_sales_2 = 1 if occ10ly_2>=4700 & occ10ly_2<=4965
replace occ_switch = 1 if occ_sales_1 == 1 & occ_sales_2 != 1
* occ 18
gen occ_office_1 = 0
replace occ_office_1 = 1 if occ10ly_1>=5000 & occ10ly_1<=5940
gen occ_office_2 = 0
replace occ_office_2 = 1 if occ10ly_2>=5000 & occ10ly_2<=5940
replace occ_switch = 1 if occ_office_1 == 1 & occ_office_2 != 1
* occ 19
gen occ_farming_1 = 0
replace occ_farming_1 = 1 if occ10ly_1>=6005 & occ10ly_1<=6130
gen occ_farming_2 = 0
replace occ_farming_2 = 1 if occ10ly_2>=6005 & occ10ly_2<=6130
replace occ_switch = 1 if occ_farming_1 == 1 & occ_farming_2 != 1
* occ 20
gen occ_construction_1 = 0
replace occ_construction_1 = 1 if occ10ly_1>=6200 & occ10ly_1<=6765
gen occ_construction_2 = 0
replace occ_construction_2 = 1 if occ10ly_2>=6200 & occ10ly_2<=6765
replace occ_switch = 1 if occ_construction_1 == 1 & occ_construction_2 != 1
* occ 21
gen occ_extraction_1 = 0
replace occ_extraction_1 = 1 if occ10ly_1>=6800 & occ10ly_1<=6940
gen occ_extraction_2 = 0
replace occ_extraction_2 = 1 if occ10ly_2>=6800 & occ10ly_2<=6940
replace occ_switch = 1 if occ_extraction_1 == 1 & occ_extraction_2 != 1
* occ 22
gen occ_install_1 = 0
replace occ_install_1 = 1 if occ10ly_1>=7000 & occ10ly_1<=7630
gen occ_install_2 = 0
replace occ_install_2 = 1 if occ10ly_2>=7000 & occ10ly_2<=7630
replace occ_switch = 1 if occ_install_1 == 1 & occ_install_2 != 1
* occ 23
gen occ_production_1 = 0
replace occ_production_1 = 1 if occ10ly_1>=7700 & occ10ly_1<=8965
gen occ_production_2 = 0
replace occ_production_2 = 1 if occ10ly_2>=7700 & occ10ly_2<=8965
replace occ_switch = 1 if occ_production_1 == 1 & occ_production_2 != 1
* occ 24
gen occ_military_1 = 0
replace occ_military_1 = 1 if occ10ly_1>=9800 & occ10ly_1<=9830
gen occ_military_2 = 0
replace occ_military_2 = 1 if occ10ly_2>=9800 & occ10ly_2<=9830
replace occ_switch = 1 if occ_military_1 == 1 & occ_military_2 != 1
*control: occ_transport gen occ_transport_1 = 0


*--------------- Outsourcing Definition --------------*
* Industry in 5613 - corresponds to Census industry code 7580
gen outsource1_1 = 0
replace outsource1_1 = 1 if indly_1 == 7580 
gen outsource1_2 = 0
replace outsource1_2 = 1 if indly_2 == 7580 

* Indicate if workers stayed outsourced or stayed not outsourced for both observations
gen stay_out1=0
replace stay_out1=1 if outsource1_1==1 & outsource1_2 == 1
gen stay_n1 = 0
replace stay_n1=1 if outsource1_1 == 0 & outsource1_2 == 0

* Indicate if the worker moved into or out of outsourcing
gen move_to_out1=0
replace move_to_out1=1 if outsource1_1 == 0 & outsource1_2 == 1
gen move_to_n1=0
replace move_to_n1=1 if outsource1_1 == 1 & outsource1_2 == 0

* Indicate if they remained in exactly the same detailed occupation
gen occ_same = 0
replace occ_same =1 if occ10ly_1==occ10ly_2

* Indicate firm size
gen size_lt10_1 = 0
replace size_lt10_1=1 if firmsize_1==1
gen size_lt10_2 = 0
replace size_lt10_2=1 if firmsize_2==1

gen size_100_499_1 = 0
replace size_100_499_1 = 1 if firmsize_1==7
gen size_100_499_2 = 0
replace size_100_499_2 = 1 if firmsize_2==7

gen size_500_999_1 = 0
replace size_500_999_1 = 1 if firmsize_1==8
gen size_500_999_2 = 0
replace size_500_999_2 = 1 if firmsize_2==8

gen size_1000_1 = 0
replace size_1000_1 = 1 if firmsize_1==9
gen size_1000_2 = 0
replace size_1000_2 = 1 if firmsize_2==9

gen size_up = 0
replace size_up = 1 if firmsize_1<firmsize_2
gen size_down = 0
replace size_down = 1 if firmsize_1>firmsize_2

* Indicate if they were classified as full-time or moved in/out of full-time between each year
gen full_1 = 0
replace full_1 = 1 if fullpart_1==1
gen full_2 = 0
replace full_2 =1 if fullpart_2==1

gen full_full = 0
replace full_full = 1 if full_1==1 & full_2==1

gen full_part = 0
replace full_part = 1 if full_1==1 & full_2 !=1

gen part_full = 0
replace part_full = 1 if full_1 !=1 & full_2==1

gen part_part = 0
replace part_part = 1 if full_1 !=1 & full_2 !=1

* generate final variables to use: non wage/income
gen age_sq = age_1^2
gen exp = age_1-16
gen exp_sq = exp^2

gen hrs_sq_1 = uhrsworkly_1^2
gen hrs_sq_2 = uhrsworkly_2^2
gen hrs_diff = uhrsworkly_1-uhrsworkly_2
gen hrs_diff_sq = (uhrsworkly_1-uhrsworkly_2)^2
gen wks_diff = wkswork1_1 - wkswork1_2
gen wks_diff_sq = (wkswork1_1 - wkswork1_2)^2
gen avg_wt = (asecwt_1 + asecwt_2)/2

* generate final variables to use: wage/income related
gen log_inc_1 = log(adj_inclongj_1)
gen log_inc_2 = log(adj_inclongj_2)
gen inc_growth = log_inc_2 - log_inc_1

gen inc_per_hr_1 = adj_inclongj_1/(wkswork1_1*uhrsworkly_1)
gen inc_per_hr_2 = adj_inclongj_2/(wkswork1_2*uhrsworkly_2)
gen log_inc_per_hr_1 = log(inc_per_hr_1)
gen log_inc_per_hr_2 = log(inc_per_hr_2)
gen inc_per_hr_growth = log_inc_per_hr_2 - log_inc_per_hr_1

* look at those who kept the same occupation but moved to outsourced industry or insourced industry
gen outsourced=0
replace outsourced=1 if move_to_out==1 & occ_switch==0
gen outsourced_strict =0
replace outsourced_strict =1 if move_to_out==1 & occ_same==1

gen insourced = 0
replace insourced =1 if move_to_n==1 & occ_switch==0
gen insourced_strict = 0
replace insourced_strict =1 if move_to_n==1 & occ_same==1


*===============================================================================================================================================================*
*========================================= Construct Transition Pr Estimates: UO, OO, ON, OU and UN, NN, NO, NU ================================================*
*===============================================================================================================================================================*
matrix results = J(6, 6, .)
local transitions NU OU NO ON UN UO
local row = 1

*---------- NU transition probability among all who were outsourced in the first year
gen tr_NU = 0 if outsource1_1 == 0 & (empstat_1 == 10 | empstat_1 == 12)
replace tr_NU = 1 if outsource1_1 == 0 & (empstat_1 == 10 | empstat_1 == 12) & (empstat_2 == 21 | empstat_2 == 22)

logit tr_NU exp exp_sq noGED some_college i.bachelors uscitizen male white black asian i.year_1 [pweight = asecwt_1], vce(robust)
margins
matrix m = r(table)
matrix results[1, 1] = m[1,1]
matrix results[1, 2] = m[2,1]

putexcel A1 = "All_edu"
putexcel A2 = (r(table)[1, 1])


logit tr_NU exp exp_sq noGED some_college uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==0, vce(robust)
margins
matrix m = r(table)
matrix results[1, 3] = m[1,1]
matrix results[1, 4] = m[2,1]

putexcel B1 = "NonCollege"
putexcel B2 = (r(table)[1, 1])

logit tr_NU exp exp_sq uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==1, vce(robust)
margins
matrix m = r(table)
matrix results[1, 5] = m[1,1]
matrix results[1, 6] = m[2,1]

putexcel C1 = "College"
putexcel C2 = (r(table)[1, 1])

*---------- OU transition probability among all who were outsourced in the first year
gen tr_OU = 0 if outsource1_1 == 1 & (empstat_1 == 10 | empstat_1 == 12)
replace tr_OU = 1 if outsource1_1 == 1 & (empstat_1 == 10 | empstat_1 == 12) & (empstat_2 == 21 | empstat_2 == 22)

logit tr_OU exp exp_sq noGED some_college i.bachelors uscitizen male white black asian i.year_1 [pweight = asecwt_1], vce(robust)
margins
matrix m = r(table)
matrix results[2, 1] = m[1,1]
matrix results[2, 2] = m[2,1]
putexcel A3 = (r(table)[1, 1])

logit tr_OU exp exp_sq noGED some_college uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==0, vce(robust)
margins
matrix m = r(table)
matrix results[2, 3] = m[1,1]
matrix results[2, 4] = m[2,1]
putexcel B3 = (r(table)[1, 1])

logit tr_OU exp exp_sq uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==1, vce(robust)
margins
matrix m = r(table)
matrix results[2, 5] = m[1,1]
matrix results[2, 6] = m[2,1]
putexcel C3 = (r(table)[1, 1])

*---------- NO transition probability 
gen tr_NO = 0 if  outsource1_1 == 0 & (empstat_1 == 10 | empstat_1 == 12)
replace tr_NO = 1 if outsource1_1 == 0 & (empstat_1 == 10 | empstat_1 == 12) & ( outsource1_2 == 1 & (empstat_2 == 10 | empstat_2 == 12) )

logit tr_NO exp exp_sq noGED some_college i.bachelors uscitizen male white black asian i.year_1 [pweight = asecwt_1], vce(robust)
margins
matrix m = r(table)
matrix results[3, 1] = m[1,1]
matrix results[3, 2] = m[2,1]
putexcel A4 = (r(table)[1, 1])

logit tr_NO exp exp_sq noGED some_college uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==0, vce(robust)
margins
matrix m = r(table)
matrix results[3, 3] = m[1,1]
matrix results[3, 4] = m[2,1]
putexcel B4 = (r(table)[1, 1])

logit tr_NO exp exp_sq uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==1, vce(robust)
margins
matrix m = r(table)
matrix results[3, 5] = m[1,1]
matrix results[3, 6] = m[2,1]
putexcel C4 = (r(table)[1, 1])

*---------- ON transition probability 
gen tr_ON = 0 if outsource1_1 == 1 & (empstat_1 == 10 | empstat_1 == 12)
replace tr_ON = 1 if outsource1_1 == 1 & (empstat_1 == 10 | empstat_1 == 12) & ( outsource1_2 == 0 & (empstat_2 == 10 | empstat_2 == 12) )

logit tr_ON exp exp_sq noGED some_college i.bachelors uscitizen male white black asian i.year_1 [pweight = asecwt_1], vce(robust)
margins
matrix m = r(table)
matrix results[4, 1] = m[1,1]
matrix results[4, 2] = m[2,1]
putexcel A5 = (r(table)[1, 1])

logit tr_ON exp exp_sq noGED some_college uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==0, vce(robust)
margins
matrix m = r(table)
matrix results[4, 3] = m[1,1]
matrix results[4, 4] = m[2,1]
putexcel B5 = (r(table)[1, 1])

logit tr_ON exp exp_sq uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==1, vce(robust)
margins
matrix m = r(table)
matrix results[4, 5] = m[1,1]
matrix results[4, 6] = m[2,1]
putexcel C5 = (r(table)[1, 1])

*---------- UN transition probability among all who were unemployed in the first year 
gen tr_UN = 0 if (empstat_1 == 21 | empstat_1 == 22) & (empstat_2 == 21 | empstat_2 == 22 | empstat_2==10 | empstat_2==12)
replace tr_UN = 1 if (empstat_1 == 21 | empstat_1 == 22) & (outsource1_2 == 0 & (empstat_2 == 10 | empstat_2 == 12))

logit tr_UN exp exp_sq noGED some_college i.bachelors uscitizen male white black asian i.year_1 [pweight = asecwt_1], vce(robust)
margins
matrix m = r(table)
matrix results[5, 1] = m[1,1]
matrix results[5, 2] = m[2,1]
putexcel A6 = (r(table)[1, 1])

logit tr_UN exp exp_sq noGED some_college uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==0, vce(robust)
margins
matrix m = r(table)
matrix results[5, 3] = m[1,1]
matrix results[5, 4] = m[2,1]
putexcel B6 = (r(table)[1, 1])

logit tr_UN exp exp_sq uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==1, vce(robust)
margins
matrix m = r(table)
matrix results[5, 5] = m[1,1]
matrix results[5, 6] = m[2,1]
putexcel C6 = (r(table)[1, 1])

*---------- UO transition probability among all who were unemployed in the first year
gen tr_UO = 0 if (empstat_1 == 21 | empstat_1 == 22)  & (empstat_2 == 21 | empstat_2 == 22 | empstat_2==10 | empstat_2==12)
replace tr_UO = 1 if (empstat_1 == 21 | empstat_1 == 22) & (outsource1_2 == 1 & (empstat_2 == 10 | empstat_2 == 12))

logit tr_UO exp exp_sq noGED some_college i.bachelors uscitizen male white black asian i.year_1 [pweight = asecwt_1], vce(robust)
margins
matrix m = r(table)
matrix results[6, 1] = m[1,1]
matrix results[6, 2] = m[2,1]
putexcel A7 = (r(table)[1, 1])

logit tr_UO exp exp_sq noGED some_college uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==0, vce(robust)
margins
matrix m = r(table)
matrix results[6, 3] = m[1,1]
matrix results[6, 4] = m[2,1]
putexcel B7 = (r(table)[1, 1])

logit tr_UO exp exp_sq uscitizen male white black asian i.year_1 [pweight = asecwt_1] if bachelors==1, vce(robust)
margins
matrix m = r(table)
matrix results[6, 5] = m[1,1]
matrix results[6, 6] = m[2,1]
putexcel C7 = (r(table)[1, 1])



*===============================================================================================================================================================*
*==================================================== Wage Growth Staying Outsourced or Staying Not Outsourced =================================================*
*===============================================================================================================================================================*

* Drop extreme (outlier) income observations
drop if inclongj_1>999999
drop if inclongj_2>999999
drop if inclongj_1<1000
drop if inclongj_2<1000

gen inc_sq = log_inc_per_hr_1^2

*  Need to exclude observations before 2003 (can't identify NAICS 5613 before this date)
* drop observations where person was not working in both yrs observed
keep if workly_1 == 2
keep if workly_2 == 2

*1999 tipped min wage was 2.13 drop reported income per hour below this
drop if inc_per_hr_1<2.13
drop if inc_per_hr_2<2.13

* keep only wage/salary workers (drop self-employed and unpaid family workers)
keep if classwly_1 >=20 & classwly_1 <= 28
keep if classwly_2 >=20 & classwly_2 <= 28

* Since income is reported for the whole year, make sure that they worked most of the year and were not multiple job holders
*--- numemps looks at changing jobs throughout the yrs
*--- incwage reports all wage/salary earnings and inclongj reports wage/salary in longest jobs
drop if numemps_1 > 1
drop if numemps_2 > 1
gen inc_ntlg_1 = incwage_1 - inclongj_1
gen inc_ntlg_2 = incwage_2 - inclongj_2
keep if inc_ntlg_1 == 0
keep if inc_ntlg_2 == 0


*%%%%% Table 9 %%%%%%%%%%%
reg log_inc_per_hr_1 outsource1_1 exp exp_sq full_1 uhrsworkly_1 hrs_sq_1 size_lt10_1 size_100_499_1 size_500_999_1 size_1000_1 noGED some_college bachelors uscitizen male white black asian occ_manage_1 occ_business_1 occ_finance_1 occ_computers_1 occ_engineertech_1 occ_soc_1 occ_comm_1 occ_legal_1 occ_educ_1 occ_arts_1 occ_health_1 occ_healthsupp_1 occ_protective_1 occ_food_1 occ_building_1 occ_care_1 occ_sales_1 occ_office_1 occ_farming_1 occ_construction_1 occ_extraction_1 occ_install_1 occ_production_1 occ_military_1 i.year_1 [pweight = asecwt_1], vce(robust)

* Save coefficient and 95% CI
matrix results_wp = r(table)
scalar b_temp     = results_wp[1, "outsource1_1"]
scalar ci_lower   = results_wp[5, "outsource1_1"]
scalar ci_upper   = results_wp[6, "outsource1_1"]

putexcel D1 = "Coef_wage_reg" 
putexcel D2 = b_temp

putexcel E1 = "CI_Lower"
putexcel E2 = ci_lower
 
putexcel F1 = "CI_Upper"
putexcel F2 = ci_upper

eststo all


reg log_inc_per_hr_1 outsource1_1 exp exp_sq full_1 uhrsworkly_1 hrs_sq_1 size_lt10_1 size_100_499_1 size_500_999_1 size_1000_1 noGED some_college uscitizen male white black asian occ_manage_1 occ_business_1 occ_finance_1 occ_computers_1 occ_engineertech_1 occ_soc_1 occ_comm_1 occ_legal_1 occ_educ_1 occ_arts_1 occ_health_1 occ_healthsupp_1 occ_protective_1 occ_food_1 occ_building_1 occ_care_1 occ_sales_1 occ_office_1 occ_farming_1 occ_construction_1 occ_extraction_1 occ_install_1 occ_production_1 occ_military_1 i.year_1 [pweight = asecwt_1] if bachelors==0, vce(robust)

* Save coefficient and 95% CI
matrix results_wp_nb = r(table)
scalar b_temp     = results_wp_nb[1, "outsource1_1"]
scalar ci_lower   = results_wp_nb[5, "outsource1_1"]
scalar ci_upper   = results_wp_nb[6, "outsource1_1"]

putexcel D3 = b_temp
putexcel E3 = ci_lower
putexcel F3 = ci_upper

eststo nobach

reg log_inc_per_hr_1 outsource1_1 exp exp_sq full_1 uhrsworkly_1 hrs_sq_1 size_lt10_1 size_100_499_1 size_500_999_1 size_1000_1 uscitizen male white black asian occ_manage_1 occ_business_1 occ_finance_1 occ_computers_1 occ_engineertech_1 occ_soc_1 occ_comm_1 occ_legal_1 occ_educ_1 occ_arts_1 occ_health_1 occ_healthsupp_1 occ_protective_1 occ_food_1 occ_building_1 occ_care_1 occ_sales_1 occ_office_1 occ_farming_1 occ_construction_1 occ_extraction_1 occ_install_1 occ_production_1 occ_military_1 i.year_1 [pweight = asecwt_1] if bachelors==1, vce(robust)

* Save coefficient and 95% CI
matrix results_wp_b = r(table)
scalar b_temp     = results_wp_b[1, "outsource1_1"]
scalar ci_lower   = results_wp_b[5, "outsource1_1"]
scalar ci_upper   = results_wp_b[6, "outsource1_1"]

putexcel D4 = b_temp
putexcel E4 = ci_lower
putexcel F4 = ci_upper

eststo bach

* Export table 9
esttab all nobach bach using Table9.tex, se label  b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) replace title("Wage Penalty Associated with Outsourcing - Cross-Sectional Regression on Log Real Wage/Salary Income per Hour") compress collabels("Coefficient" "S.E." "Coefficient" "S.E." "Coefficient" "S.E.") cells("b(star fmt(3)) se(par fmt(3))") keep(outsource1_1 exp exp_sq full_1 uhrsworkly_1 hrs_sq_1 size_lt10_1 size_100_499_1 size_500_999_1 size_1000_1 noGED some_college bachelors uscitizen male white black asian occ_manage_1 occ_business_1 occ_finance_1 occ_computers_1 occ_engineertech_1 occ_soc_1 occ_comm_1 occ_legal_1 occ_educ_1 occ_arts_1 occ_health_1 occ_healthsupp_1 occ_protective_1 occ_food_1 occ_building_1 occ_care_1 occ_sales_1 occ_office_1 occ_farming_1 occ_construction_1 occ_extraction_1 occ_install_1 occ_production_1 occ_military_1 _cons) varlabels(outsource1_1 "In employment services" exp "Potential experience" exp_sq "Potential experience squared" full_1 "Full time" uhrsworkly_1 "Usual hours worked per week" hrs_sq_1 "Usual hours per week squared" size_lt10_1 "Firm size $<$ 10" size_100_499_1 "Firm size 100-499" size_500_999_1 "Firm size 500-999" size_1000_1 "Firm size 1,000+" noGED "Less than high school" some_college "Some college" bachelors "Bachelor's degree or more" uscitizen "U.S. citizen" male "Male" white "White" black "Black" asian "Asian" occ_manage_1 "Management, business, science, and arts" occ_business_1 "Business operations specialists" occ_finance_1 "Financial specialists" occ_computers_1 "Computer and mathematical" occ_engineertech_1 "Architecture, engineering, technicians" occ_soc_1 "Life, physical, and social science" occ_comm_1 "Community and social services" occ_legal_1 "Legal" occ_educ_1 "Education, training, and library" occ_arts_1 "Arts, design, entertainment, sports, media" occ_health_1 "Healthcare practitioners and technical" occ_healthsupp_1 "Healthcare support" occ_protective_1 "Protective service" occ_food_1 "Food preparation and serving" occ_building_1 "Building, grounds cleaning, maintenance" occ_care_1 "Personal care and service" occ_sales_1 "Sales and related" occ_office_1 "Office and administrative support" occ_farming_1 "Farming, Fishing, and Forestry" occ_construction_1 "Construction" occ_extraction_1 "Extraction" occ_install_1 "Installation, maintenance, and repair" occ_production_1 "Production" occ_military_1 "Military specific" _cons "Constant") nomtitles nonumbers stats(N r2_a, fmt(%15.0fc 3) labels("Observations" "Adjusted R$^2$")) 

* Table 9 robustness - leave out office occupations
reg log_inc_per_hr_1 outsource1_1 exp exp_sq full_1 uhrsworkly_1 hrs_sq_1 size_lt10_1 size_100_499_1 size_500_999_1 size_1000_1 noGED some_college bachelors uscitizen male white black asian occ_manage_1 occ_business_1 occ_finance_1 occ_computers_1 occ_engineertech_1 occ_soc_1 occ_comm_1 occ_legal_1 occ_educ_1 occ_arts_1 occ_health_1 occ_healthsupp_1 occ_protective_1 occ_food_1 occ_building_1 occ_care_1 occ_sales_1 occ_farming_1 occ_construction_1 occ_extraction_1 occ_install_1 occ_production_1 occ_military_1 i.year_1 [pweight = asecwt_1] if occ_office_1==0, vce(robust)
* adjusted R^2
di e(r2_a)


*%%%%%%% Table 10 %%%%%%%%
reg inc_per_hr_growth stay_out1 move_to_n1 move_to_out1 exp exp_sq hrs_diff hrs_diff_sq wks_diff wks_diff_sq size_lt10_2 size_100_499_2 size_500_999_2 size_1000_2 size_up size_down noGED some_college bachelors uscitizen male white black asian occ_manage_2 occ_business_2 occ_finance_2 occ_computers_2 occ_engineertech_2 occ_soc_2 occ_comm_2 occ_legal_2 occ_educ_2 occ_arts_2 occ_health_2 occ_healthsupp_2 occ_protective_2 occ_food_2 occ_building_2 occ_care_2 occ_sales_2 occ_office_2 occ_farming_2 occ_construction_2 occ_extraction_2 occ_install_2 occ_production_2 occ_military_2 log_inc_per_hr_1 inc_sq i.year_1 [pweight = asecwt_2], vce(robust)

* Save coefficient and 95% CI
matrix results_wg = r(table)
scalar b_temp     = results_wg[1, "stay_out1"]
scalar ci_lower   = results_wg[5, "stay_out1"]
scalar ci_upper   = results_wg[6, "stay_out1"]

putexcel G1 = "Coef_wage_reg" 
putexcel G2 = b_temp

putexcel H1 = "CI_Lower"
putexcel H2 = ci_lower
 
putexcel I1 = "CI_Upper"
putexcel I2 = ci_upper

eststo growth_all

reg inc_per_hr_growth stay_out1 move_to_n1 move_to_out1 exp exp_sq hrs_diff hrs_diff_sq wks_diff wks_diff_sq size_lt10_2 size_100_499_2 size_500_999_2 size_1000_2 size_up size_down noGED some_college uscitizen male white black asian occ_manage_2 occ_business_2 occ_finance_2 occ_computers_2 occ_engineertech_2 occ_soc_2 occ_comm_2 occ_legal_2 occ_educ_2 occ_arts_2 occ_health_2 occ_healthsupp_2 occ_protective_2 occ_food_2 occ_building_2 occ_care_2 occ_sales_2 occ_office_2 occ_farming_2 occ_construction_2 occ_extraction_2 occ_install_2 occ_production_2 occ_military_2 log_inc_per_hr_1 inc_sq i.year_1 [pweight = asecwt_2] if bachelors==0, vce(robust)

* Save coefficient and 95% CI
matrix results_wg_nb = r(table)
scalar b_temp     = results_wg_nb[1, "stay_out1"]
scalar ci_lower   = results_wg_nb[5, "stay_out1"]
scalar ci_upper   = results_wg_nb[6, "stay_out1"]

putexcel G3 = b_temp
putexcel H3 = ci_lower
putexcel I3 = ci_upper

eststo growth_nobach

reg inc_per_hr_growth stay_out1 move_to_n1 move_to_out1 exp exp_sq hrs_diff hrs_diff_sq wks_diff wks_diff_sq size_lt10_2 size_100_499_2 size_500_999_2 size_1000_2 size_up size_down uscitizen male white black asian occ_manage_2 occ_business_2 occ_finance_2 occ_computers_2 occ_engineertech_2 occ_soc_2 occ_comm_2 occ_legal_2 occ_educ_2 occ_arts_2 occ_health_2 occ_healthsupp_2 occ_protective_2 occ_food_2 occ_building_2 occ_care_2 occ_sales_2 occ_office_2 occ_farming_2 occ_construction_2 occ_extraction_2 occ_install_2 occ_production_2 occ_military_2 log_inc_per_hr_1 inc_sq i.year_1 [pweight = asecwt_2] if bachelors==1, vce(robust)

* Save coefficient and 95% CI
matrix results_wg_b = r(table)
scalar b_temp     = results_wg_b[1, "stay_out1"]
scalar ci_lower   = results_wg_b[5, "stay_out1"]
scalar ci_upper   = results_wg_b[6, "stay_out1"]

putexcel G4 = b_temp
putexcel H4 = ci_lower
putexcel I4 = ci_upper

eststo growth_bach

esttab growth_all growth_nobach growth_bach using Table10.tex, se label  b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) replace title("Real Wage Growth Differences Based on Labor Outsourcing") compress collabels("Coefficient" "S.E." "Coefficient" "S.E." "Coefficient" "S.E.") cells("b(star fmt(3)) se(par fmt(3))") keep(stay_out1 move_to_n1 move_to_out1 exp exp_sq hrs_diff hrs_diff_sq wks_diff wks_diff_sq size_lt10_2 size_100_499_2 size_500_999_2 size_1000_2 size_up size_down noGED some_college bachelors uscitizen male white black asian log_inc_per_hr_1 inc_sq _cons) varlabels(stay_out1 "In employment services" move_to_n1 "Move out of employment services" move_to_out1 "Move into employment services" exp "Potential experience" exp_sq "Potential experience squared" hrs_diff "Hours difference" hrs_diff_sq "Hours difference squared" wks_diff "Weeks difference" wks_diff_sq "Weeks difference squared" size_lt10_2 "Firm size $<$ 10" size_100_499_2 "Firm size 100-499" size_500_999_2 "Firm size 500-999" size_1000_2 "Firm size 1000+" size_up "Firm size increase" size_down "Firm size decrease" noGED "Less than high school" some_college "Some college" bachelors "Bachelor's degree or more" uscitizen "U.S. citizen" male "Male" white "White" black "Black" asian "Asian" log_inc_per_hr_1 "Initial log income per hr" inc_sq "Initial log income per hr squared" _cons "Constant") nomtitles nonumbers stats(N r2_a, fmt(%15.0fc 3) labels("Observations" "Adjusted R$^2$")) 

* robustness - exclude office occupations
reg inc_per_hr_growth stay_out1 move_to_n1 move_to_out1 exp exp_sq hrs_diff hrs_diff_sq wks_diff wks_diff_sq size_lt10_2 size_100_499_2 size_500_999_2 size_1000_2 size_up size_down noGED some_college bachelors male white black asian uscitizen occ_manage_2 occ_business_2 occ_finance_2 occ_computers_2 occ_engineertech_2 occ_soc_2 occ_comm_2 occ_legal_2 occ_educ_2 occ_arts_2 occ_health_2 occ_healthsupp_2 occ_protective_2 occ_food_2 occ_building_2 occ_care_2 occ_sales_2 occ_farming_2 occ_construction_2 occ_extraction_2 occ_install_2 occ_production_2 occ_military_2 log_inc_per_hr_1 inc_sq if occ_office_2==0 [pweight = asecwt_2], vce(robust)
di e(r2_a)

*%%%%%%% Wage Growth %%%%%%%%%%%
mean inc_per_hr_growth [pweight = asecwt_1]

* Save mean overall wage growth
scalar mean_growth = r(table)[1,1]
matrix results = mean_growth

putexcel J1 = "m8_avg_wage_growth" 
putexcel J2 = matrix(results)


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%% CPS CWS for Additional Data Moments %%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use CPS_CWS_NextMonth.dta, clear

sort cpsidp year month 
order cpsidp year month

 * Count the number of times the individual is observed
quietly by cpsidp:  gen dup = cond(_N==1,0,_n)
order cpsidp year month dup

*================== Age 20 Unemployment Rate 
gen unemp = 0 if empstat==10 | empstat==12
replace unemp = 1 if empstat==21| empstat==22

logit unemp i.year [pweight = cwsuppwt] if year>2002 & year<2020 & age==20
margins 

scalar urate20 = r(table)[1,1]
matrix results = urate20

putexcel K1 = "m10_urate_20" 
putexcel K2 = matrix(results)


* Indicate if they were outsourced or not
gen agency = 1 if cwpdtag==02
replace agency=0 if cwpdtag==01
replace agency=agency[_n-1] if dup==2 & cpsidp==cpsidp[_n-1]

gen contractfirm = 1 if cwcontract==02
replace contractfirm=0 if cwcontract==01
replace contractfirm=contractfirm[_n-1] if dup==2 & cpsidp==cpsidp[_n-1]

gen outsourced = 1 if (agency==1 | contractfirm==1)
replace outsourced = 0 if (agency==0 & contractfirm==0)


logit outsourced i.year [pweight = cwsuppwt] if year>2002 & age>=20, vce(robust)
margins

scalar pc_do = r(table)[1,1]
matrix results = pc_do

putexcel L1 = "m3_percent_outsourced" 
putexcel L2 = matrix(results)


* MONTHLY_EU rate
* Drop if individual is only observed once
drop if dup==0

* Based on Timing of observations, generate indicators for employment status
gen empstat_1 = empstat if dup==1
replace empstat_1 = empstat[_n-1] if dup==2 & cpsidp==cpsidp[_n-1]

gen empstat_2 = empstat if dup==2
replace empstat_2 = empstat[_n+1] if dup==1 & cpsidp==cpsidp[_n+1]

gen empsame_2 = empsame if dup==2
drop if missing(outsourced)

* Now look at month-month empstat
drop if dup==2
drop if (empstat_1 !=10 & empstat_1 !=12)
drop if age<18

* Controls
gen exp = age-18
gen exp_sq = exp^2

* EU
gen EU = 1 if (empstat_2 == 21 | empstat_2 == 22)
replace EU = 0 if (empstat_2 == 10 | empstat_2==12)

logit EU exp exp_sq i.wkstat i.educ i.race i.sex i.year [pweight = cwsuppwt] if year>2002 & age>=20, vce(robust)
margins 


scalar EUrate = r(table)[1,1]
matrix results = EUrate

putexcel M1 = "m5_EU_rate" 
putexcel M2 = matrix(results)

